﻿using System;
using System.Linq;
using System.Collections;
using ASPNETVN.PORTAL.Components.Configuration;
using ASPNETVN.PORTAL.Components.UI;
using System.Collections.Generic;
using ASPNETVN.PORTAL.Components.Share;
using ASPNETVN.PORTAL.Modules.CMS.Components;

namespace ASPNETVN.PORTAL.Modules.CMS.Home.Controls
{
    public partial class Style10 : ModulePage
    {
        protected string GetUrl(int itemID)
        {
            Guid tabID = Guid.Empty;
            PortalDataSet ds = PortalConfiguration.GetPortalDataSet();

            foreach (PortalDataSet.TabRow tab in ds.Tab.Rows)
            {
                foreach (PortalDataSet.ModuleRow module in tab.GetModuleRows())
                {
                    if (module.ID == new Guid(this.Settings["CMS.ModuleID"].ToString()))
                    {
                        return string.Format("~/Default.aspx?tabid={0}&mid={1}&itemid={2}&page=detail", tab.ID, module.ID, itemID);
                    }
                }
            }
            return string.Format("~/Default.aspx");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                Hashtable settings = this.Settings;
                Guid mid = new Guid(settings["CMS.ModuleID"].ToString());

                iframe.Attributes.Add("src", Global.GetApplicationPath() + "/Modules/ASPNETVN.PORTAL.Modules.CMS.HOME/Pages/SlideShow9.aspx?mid=" + mid);

                //Lấy số tin tức mới nhất trong 5 Groups theo cấu hình số tin được lấy trong mỗi Groups.
                int soTinHienThiTrenTrang = int.Parse(settings["CMS.RecordOnRightPage"].ToString());

                List<Category> listGroups = Category.SelectCollectionAll(mid).Where(x => x.ParentID != 0).ToList();
                List<Item> listItems = new List<Item>();

                for (int i = 0; i < listGroups.Count; i++)
                {
                    listItems.AddRange(Item.SelectCollectionBy_CategoryID(listGroups[i].ID).OrderByDescending(x => x.PublishedDate).ToList());
                }

                //Get limit news
                if (listItems.Count > 5)
                {
                    listItems = listItems.OrderByDescending(x => x.PublishedDate).Skip(5).Take(soTinHienThiTrenTrang).ToList();
                }
                else
                    //Sort
                    listItems = listItems.OrderByDescending(x => x.PublishedDate).ToList();

                // Get one type item for home.
                //Repeater1.DataSource = Components.Item.GetItemsByDisplayType(moduleID, "ONE");
                Repeater1.DataSource = listItems;
                Repeater1.DataBind();

                //Repeater1.DataSource = Components.Item.GetItemsByDisplayType(mid, "TWO");
                //Repeater1.DataBind();
            }
        }

        public string LimitTitle(string title, int limitNumber)
        {
            if (title.Trim().Length > limitNumber)
            {
                return title.Trim().Substring(0, 300);
            }

            return title;
        }
    }
}